import GameCtrl, { GameRoleCtrl } from "../../../game/GameCtrl";
import BaseWidget from "../../BaseWidget";
import GameMapWidget from "../GameMapWidget";

const {ccclass, property, menu} = cc._decorator;

@ccclass
@menu("Widget/Game/GameTouchControllerWidget")
export default class GameTouchControllerWidget extends BaseWidget{
    @property(cc.Graphics)
    graphics: cc.Graphics = null;

    @property(GameMapWidget)
    gameMapWidget: GameMapWidget = null;

    private _enabled = false;
    private _path: cc.Vec2[] = [];

    public init(){
        this._enabled = true;
        this._path.length;

        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouch, this);
    }

    public stop(){
        this._enabled = false;
        this.clear();
    }

    public clear(){
        this._path.length = 0;
        this._updatePathUI();
    }

    public getNextPoint(){
        if(this._path.length == 0){
            return null;
        }
        let ret = this._path.pop();

        this._updatePathUI();

        return ret;
    }

    private onTouch(event: cc.Event.EventTouch){
        if(!this._enabled) return;


        let worldPos = event.getLocation();
        let coord = this.gameMapWidget.pos2Coord(worldPos);
        let path = GameCtrl.getInstance().getPathTo(coord);

        if(path != null){
            this._path = path;
            this._updatePathUI();
        }
    }

    private _updatePathUI(){
        this.graphics.clear();

        for(let i = 0; i < this._path.length - 1; i++){
            let beginPoint = this._path[i];
            let endPoint = this._path[i + 1];
            let pos = this.gameMapWidget.coord2Pos(beginPoint.x, beginPoint.y);
            let pos1 = this.gameMapWidget.coord2Pos(endPoint.x, endPoint.y);
            this._drawDashLine(pos, pos1, 10, 10);
        }
        this.graphics.stroke();
    }

    private _test(){
        let path = [cc.v2(0, 0), cc.v2(100, 0), cc.v2(100, 100), cc.v2(0, 100), cc.v2(0, 0)];

        this.graphics.clear();
        for(let i = 0; i < path.length - 1; i++){
            this._drawDashLine(path[i], path[i+1], 10, 4);
        }
        this.graphics.stroke();
    }

    private _drawDashLine(from: cc.Vec2, to: cc.Vec2, maxLength: number, dashLength: number){
        let dir = to.sub(from).normalize();
        let length = to.sub(from).len();
        let lengths = [];
        let begin = 0;
        while(true){
            if(length - begin < maxLength){
                lengths.push(begin);
                lengths.push(length);
                break;
            }else{
                lengths.push(begin);
                begin += maxLength;
                lengths.push(begin);
                begin += dashLength;
            }
        }

        for(let i = 0; i < lengths.length; i+=2){
            let beginLength = lengths[i];
            let endLength = lengths[i+1];
            let beginPos = from.add(dir.mul(beginLength));
            let endPos = from.add(dir.mul(endLength));

            this.graphics.moveTo(beginPos.x, beginPos.y);
            this.graphics.lineTo(endPos.x, endPos.y);
        }
    }
}